package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import javax.imageio.stream.ImageInputStream;
import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {

       /* int sum = 0, c = 0;
        if (leftBorder > rightBorder) {
            c = rightBorder;
            rightBorder = leftBorder;
            leftBorder = c;
        }
        c = rightBorder - leftBorder;
        if (c == 1 || c == 0) {
            return sum;
        }
        leftBorder++;
        for (; leftBorder < rightBorder; leftBorder++) {
            if (leftBorder % 2 == 0) {
                sum += leftBorder;
            }
        }
        return sum;
    }*/
       /* int sum = 0, c = 0;
        if (leftBorder > rightBorder) {
            c = leftBorder;
            leftBorder = rightBorder;
            rightBorder = c;
            for (int i = leftBorder; i < rightBorder; i++) {
                if (i % 2 == 0) {
                    sum += i;
                }
            }
        }

        return sum;
    }*/
        int sum =0 ;
        if(leftBorder<rightBorder){
            for(int i = leftBorder;i<=rightBorder;i++){
                if(i%2 == 0){
                    sum=sum+i;
                }
            }
        }else{
            for(int i =rightBorder;i<=leftBorder;i++){
                if (i%2==0){
                    sum=sum+i;
                }
            }
        }
        return sum;
    }


    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int sum =0 ;
        if(leftBorder<rightBorder){
            for(int i = leftBorder;i<=rightBorder;i++){
                if(i%2 != 0){
                    sum=sum+i;
                }
            }
        }else{
            for(int i =rightBorder;i<=leftBorder;i++){
                if (i%2 !=0){
                    sum=sum+i;
                }
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
      int sum = 0;
        Integer[] integers = (Integer[]) arrayList.toArray();
        for (int i =0;i<integers.length;i++){
            int c = integers[i]*3+2;
            sum +=c;
        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        for (int i =0;i<integers.length;i++){
            if(integers[i]%2!=0){
                int c = integers[i]*3+2;
                integers[i]=c;
            }
        }
        List<Integer> list= Arrays.asList(integers);
        return list;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int sum = 0;
        for (int i =0;i<integers.length;i++){
            if(integers[i]%2!=0){
                int c = integers[i]*3+5;
                sum +=c;
            }
        }
        return  sum;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int a=0;
        int b=0;
        int c=0;
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2==0){
                a+=integers[i];
                b++;
                c=a/b;
            }
        }
        return c;

    }


    public double getAverageOfEven(List<Integer> arrayList) {
        Integer[] integers = (Integer[]) arrayList.toArray();
        int a=0;
        int b=0;
        int c=0;
        for(int i=0;i<integers.length;i++){
            if(integers[i]%2==0){
                a+=integers[i];
                b++;
                c=a/b;
            }
        }
        return c;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        List<Integer> list = new ArrayList<>();
        for ( Integer arr:arrayList) {
            if (arr%2==0){
                list.add(arr);
            }
        }
        boolean b= false;
        for (Integer l:list) {
            if(l==specialElment){
                b= true;
            }
        }
        return b;
    }


    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for ( Integer arr:arrayList) {
            if (arr%2==0){
                list.add(arr);
            }
        }
        Set<Integer> middleHashSet = new HashSet<Integer>(list);

        List<Integer> afterHashSetList = new ArrayList<Integer>(middleHashSet);
        return  afterHashSetList;
    }


    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        List<Integer> odd = new ArrayList<Integer>();
        List<Integer> even = new ArrayList<Integer>();

        int temp = 0;
        for (int j=0; j<arrayList.size(); j++) {
            temp= arrayList.get(j);
            if(temp%2==0) {
                even.add(temp);
            }else {
                odd.add(temp);
            }
            Collections.sort(even);
            Collections.sort(odd,Collections.reverseOrder());

            int m = 0;
            for(int i=0; i < even.size(); i++){
                arrayList.set(m, even.get(i));
                m++;
            }
            for(int jj = 0;jj < odd.size();jj++){
                arrayList.set(m, odd.get(jj));
                m++;
            }
        }
        return arrayList;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
         List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size() - 1; i++) {
            int r = (arrayList.get(i) + arrayList.get(i + 1)) * 3;
            list.add(r);
        }
        return list;
    }
}
